package recursion;

import java.util.Scanner;

public class WaveDeep {
	
	public static int test_case;
	
	public static int T;
	public static int H;
	public static int W;
	public static int S;
	
	public static int[][] M;
	public static int cnt;
	public static int deep = 0;
	public static int peak = 0;
	public static int stkCnt = 0;

	public static void main(String[] args) throws Exception {
		
		Scanner sc = new Scanner(System.in);
		
		T = sc.nextInt();
		
		for (test_case = 1; test_case <= T; test_case++){
			H = sc.nextInt();
			W = sc.nextInt();
			S = sc.nextInt();
			M = new int[H+1][W+1];
			for(int i=0; i<H; i++){
				for (int j=0; j<W; j++){
					M[i][j] = sc.nextInt();
				}
			}
			//////////////////////////////////
			int max = Integer.MIN_VALUE;
			for (int i=0; i<H; i++){
				for (int j=0; j<W; j++){
					if (M[i][j] <= S){
						M[i][j] = -1;
						continue;
					}
					cnt = 0;
					find(i,j);
					if (cnt > max){
						max = cnt;
					}	
				}
			}
			
			
			System.out.println("stkCnt==============" + stkCnt);
			System.out.println("peak================" + peak);
			System.out.println("#" + test_case + " " + max);
			//////////////////////////////////
			
		}
	}
	
	public static void find(int i, int j){
		stkCnt++;
		deep++;
		if (deep > peak){
			peak = deep;
		}
		if (i<0 || i>H-1){
			deep--;
			return;
		}
		
		if (j<0 || j>W-1){
			deep--;
			return;
		}
		
		if (M[i][j] == -1){
			deep--;
			return;
		}
		
		if (M[i][j] <= S){
			deep--;
			M[i][j] = -1;
			return;
		}
		
		cnt++;
		M[i][j] = -1;
		
		find(i-1,j);
		find(i,j+1);
		find(i+1,j);
		find(i,j-1);
		deep--;
	}

}
